package com.example.bluetooth;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.BatteryManager;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.webviewtest.MainActivity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MyBlueToothService extends Service {
    public BLEPositioning blePositioning;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        //app启动时开启蓝牙扫描功能，上传蓝牙数据至5g运营平台
//         blePositioning = new BLEPositioning(this);
//        blePositioning = new BLEPositioning(getApplication());
        Timer timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
//                    blePositioning.stopScan();
//                    blePositioning.startScan();
                    if (ContextCompat.checkSelfPermission(MainActivity.instance, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED) {
                        // 权限未授予，需要请求权限
                        ActivityCompat.requestPermissions(MainActivity.instance,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                1);
                    } else {
                        // 权限已授予，可以进行文件夹创建操作
                    }
                    //每10s查询一次电量和充电状态将其保存在本地
                    BatteryManager manager = (BatteryManager) MainActivity.instance.getSystemService(BATTERY_SERVICE);
            //        manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
            //        manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
            //        manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
            //        manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);///当前电量百分比
                    int batteryStatus = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);///充电状态
                    String status = "";
                    if (batteryStatus==2) {
                        status = "充电中";
                    } else if (batteryStatus == 3) {
                        status = "未充电";
                    }
                    int batteryValue = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);///充电状态
                    Date date = new Date();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    String time = dateFormat.format(date.getTime());
                    String s = time + "  " + status + "  " + batteryValue+"\n";
                    System.out.println(s);
                    MainActivity.instance.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
//                            Toast.makeText(MainActivity.instance, s, Toast.LENGTH_SHORT).show();
                        }
                    });
                    try {
                        String logPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/battery/batteryLog.txt";
                        File file = new File(logPath);
                        if (!file.exists()) {
                            file.getParentFile().mkdirs();
                            file.createNewFile();
                        }
                        FileWriter writer = new FileWriter(logPath, true); // 追加模式
                        writer.write(s);
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                    }
                }catch (Exception e) {
                    System.out.println(e);
//                    timer1.cancel();
                }
            }
        },0,10000);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        blePositioning.stopScan();
    }

}
